package edu.thu.studio.ormdesigner.model.diagram;

import java.util.Iterator;

import org.eclipse.ui.PartInitException;

import com.thoughtworks.xstream.annotations.XStreamOmitField;

import edu.thu.studio.ormdesigner.editor.ORMMultiPageEditor;
import edu.thu.studio.ormdesigner.model.ObjectSet;

public class DiagramSet extends ObjectSet<Diagram> {
    
    private static final long serialVersionUID = -6395092030037391244L;
    
    @XStreamOmitField()
    private ORMMultiPageEditor ormMultiPageEditor;
    
    public void merge(Diagram newDiagram) {
        Iterator<Diagram> iter = this.iterator();
        while (iter.hasNext()) {
            Diagram oldDiagram = iter.next();
            if (null != oldDiagram.getId() && null != newDiagram.getId()) {
                if (oldDiagram.getId().equals(newDiagram.getId())) {
                    oldDiagram.merge(newDiagram);
                }
            }
        }
        if (null != ormMultiPageEditor) {
            ormMultiPageEditor.updateGraphicalEditor(newDiagram);
        }
        
    }
    
    public void addObject(Object object) {
        super.addObject(object);
        try {
            if (null != ormMultiPageEditor) {
                ormMultiPageEditor.addGraphicalEditor((Diagram) object);
            }
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    }
    
    public int remove(Object object) {
        if (null != ormMultiPageEditor) {
            ormMultiPageEditor.removeGraphicalEditor((Diagram) object);
        }
        return super.remove(object);
    }
    
    public void setEditor(ORMMultiPageEditor ormMultiPageEditor) {
        this.ormMultiPageEditor = ormMultiPageEditor;
    }
    
}